﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Web;
using Microsoft.Reporting.WebForms;

using KPWeb.Data;
namespace KPWeb.Function
{
    /// <summary>
    /// Summary description for Barcode
    /// </summary>
    public class Barcode : IHttpHandler
    {
        public static string TextToBarCode = "";
        public static Boolean IsVerticalTextToBarCode = false;
        //private static string ValidateMod43(string barcode)
        //{
        //    //*12345C*    C jeden znak sumy kontrolnej
        //    int subtotal = 0;
        //    const string charSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";

        //    foreach (char c in barcode)

        //        subtotal += charSet.IndexOf(c);

        //    return charSet[subtotal % 43].ToString();

        //}

        public void ProcessRequest(HttpContext context)
        {
            int width;
            int height;
            StringFormat drawFormat = new StringFormat();
            if (!IsVerticalTextToBarCode)
            {
                width = 250;
                height = 30;
                drawFormat.FormatFlags = StringFormatFlags.NoWrap;
            }
            else
            {
                width = 35;
                height = 250;
                drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
            }
            System.Drawing.Image bmp = new Bitmap(width, height);
            string text = TextToBarCode;
            text = "*" + text + "*";
            PrivateFontCollection pfc = new PrivateFontCollection();
            pfc.AddFontFile(System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/"), "Styles", "free3of9.ttf"));
            FontFamily family = new FontFamily("Free 3 of 9", pfc);
            Font c39Font = new Font(family, 48);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            SizeF barCodeSize = g.MeasureString(text, c39Font);
            g.DrawString(text, c39Font, new SolidBrush(Color.Black), 0, 0, drawFormat);
            //SizeF textSize = g.MeasureString(text, new Font("Arial Black", 12));
            //g.DrawString(text.Replace('*', ' '), new Font("Arial Black", 12), new SolidBrush(Color.Black), 0, barCodeSize.Height - 12);
            context.Response.Clear();
            bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
            context.Response.Flush();


        }
        public static ImagesSet[] GetBarcode()
        {
            int width;
            int height;
            StringFormat drawFormat = new StringFormat();
          
            if (!IsVerticalTextToBarCode)
            {
                width = 300;
                height = 25;
                drawFormat.FormatFlags = StringFormatFlags.NoWrap;
            }
            else
            {
                width = 30;
                height = 300;
                drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
            }

            System.Drawing.Image bmp = new Bitmap(width, height);
            string text = TextToBarCode;
            text = "*" + text + "*";
            PrivateFontCollection pfc = new PrivateFontCollection();
            pfc.AddFontFile(System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/"), "Styles", "free3of9.ttf"));
            FontFamily family = new FontFamily("Free 3 of 9", pfc);
            Font c39Font = new Font(family, 48);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            SizeF barCodeSize = g.MeasureString(text, c39Font);
            g.DrawString(text, c39Font, new SolidBrush(Color.Black), 0, 0, drawFormat);
            //SizeF textSize = g.MeasureString(text, new Font("Arial Black", 12));
            //g.DrawString(text.Replace('*', ' '), new Font("Arial Black", 12), new SolidBrush(Color.Black), 0, barCodeSize.Height - 12);
            MemoryStream ms=new MemoryStream();
            bmp.Save(ms, ImageFormat.Png);

            ImagesSet img = new ImagesSet();
            img.Content = ms.ToArray();
            img.ID = int.Parse(TextToBarCode);// 1;
            return new ImagesSet[1] { img };
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }



        //public static void RaportBarCodePDF(int id, string raportName)//,string[][] parametry)//"WniosekUrlopowy.rdlc"
        //{
        //    KPWeb.Function.Barcode.TextToBarCode = id.ToString();// "019876549";
        //    KPWeb.Function.Barcode.IsVerticalTextToBarCode = true;
        //    List<ReportDataSource> rds = new List<ReportDataSource>();
        //    rds.Add(new ReportDataSource("DataSet1", KPWeb.Function.Barcode.GetBarcode()));
        //    KPWeb.Function.Barcode.IsVerticalTextToBarCode = false;
        //    rds.Add(new ReportDataSource("DataSet2", KPWeb.Function.Barcode.GetBarcode()));
        //    int iTemp = id % 10000000;
        //    string sTemp = "";

        //    vKP_DrukWniosekUrlopowy[] t = UrlopyK.GetDrukWniosekUrlopowy(iTemp);
        //    rds.Add(new ReportDataSource("DataSet3", UrlopyK.GetDrukWniosekUrlopowy(iTemp)));



        //    List<Microsoft.Reporting.WebForms.ReportParameter> l = new List<ReportParameter>();
        //    l.Add(new Microsoft.Reporting.WebForms.ReportParameter("P1", KPWeb.Function.Barcode.TextToBarCode));
        //    l.Add(new Microsoft.Reporting.WebForms.ReportParameter("P2",
        //        FunctionGlobal.UppercaseFirstWords(t.Single().NazwiskoImie)));
        //    if (t.Single().Zastepstwo != null) sTemp = FunctionGlobal.UppercaseFirstWords(t.Single().Zastepstwo);

        //    l.Add(new Microsoft.Reporting.WebForms.ReportParameter("P3",sTemp));
        //    //for (int i = 0; i < parametry.Length; i++)
        //    //{
        //    //    l.Add(new Microsoft.Reporting.WebForms.ReportParameter(parametry[0][i],parametry[1][i]));
        //    //}

        //    //Microsoft.Reporting.WebForms.LocalReport report = KPWeb.Reports.LocalReport.GenerujRaport("WniosekUrlop.rdlc", rds, l);
        //    Microsoft.Reporting.WebForms.LocalReport report = KPWeb.Reports.LocalReport.GenerujRaport(raportName, rds, l);
        //    //    KPWeb.Reports.LocalReport.Export(report, "Excel", "RankingNauczycieli.xls");
        //    KPWeb.Reports.LocalReport.Export(report, "PDF", raportName.Substring(0,raportName.Length-4)+"pdf");
        //}


    }
}